package com.luck.easyfoodbatch.mvp.order;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.order
 * email：893855882@qq.com
 * data：16/11/11
 */
public class DeleteOrderPresenter implements DeleteOrderModel {

    private final String TAG = getClass().getSimpleName();
    private DeleteOrderView orderView;
    private Context mContext;

    public DeleteOrderPresenter(DeleteOrderView orderView, Context mContext) {
        this.orderView = orderView;
        this.mContext = mContext;
    }

    @Override
    public void requestDeleteOrder(int type, String order_id) {
        HttpParams params = new HttpParams();
        String path = "";
        if (type == 1) {
            // 删除退货订单
            params.put("rid", order_id);
            path = HttpUtil.DELETE_REFUND;
        } else {
            // 删除订单
            params.put("order_id", order_id);
            path = HttpUtil.DELETE_ORDER;
        }
        OkHttpUtils.post(path)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, Collect.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        orderView.deleteOrderSuccess((Collect) data);
                        orderView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        orderView.hideLoading();
                        orderView.httpStatusError();
                    }
                });
    }
}
